from selenium import webdriver
import time
import pandas as pd
driver = webdriver.Chrome(executable_path='./chromedriver-2')
driver.get('http://qikan.cqvip.com/index.html?from=Qikan_Search_Index')

search_input = driver.find_element_by_id('searchKeywords')
search_input.send_keys('高血压meta')
btn = driver.find_element_by_id('btnSearch')
btn.click()
title = ''
judge = 0
authors = []
author = ''
source = ''
objs = []
try:
    for i in range(6):
        normal_window = driver.current_window_handle
        all_Handles = driver.window_handles

        for handle in all_Handles:
            if handle != normal_window:
                driver.switch_to.window(handle)
            dls = driver.find_elements_by_xpath('//*[@id="remark"]/dl')
            for dl in dls:
                obj = {'title': '', 'authors': '', 'abstract': '', 'keyWords': '', 'source': ''}
                dts = dl.find_elements_by_xpath('./dt')
                for dt in dts:
                    aes = dt.find_elements_by_xpath('./a')
                    for a in aes:
                        title = a.get_attribute('textContent')
                        obj['title'] = title

                dds = dl.find_elements_by_xpath('./dd[3]')
                for dd in dds:
                    spans = dd.find_elements_by_xpath('./span[1]')
                    for span in spans:
                        st = span.get_attribute('textContent').split()
                        if len(st) != 0:
                            del(st[0])
                            st = ','.join(st)
                            author = st
                            obj['authors'] = author
                        else:
                            obj['authors'] = ''

                spans_abs = dl.find_elements_by_xpath('./dd[4]/span/span[2]')
                if len(spans_abs) == 0:
                    spans_abs = dl.find_elements_by_xpath('./dd[4]/span/span[1]')

                for sp in spans_abs:
                    st_abs = sp.get_attribute('textContent').split()
                    st_abs = ''.join(st_abs)
                    obj['abstract'] = st_abs

                span_keys = dl.find_elements_by_xpath('./dd[5]/span/span')
                st_keys = []
                for span_key in span_keys:
                    ass = span_key.find_elements_by_xpath('./a')
                    if len(ass) != 0:
                        for a in ass:
                            st_key = a.get_attribute('textContent')
                            st_keys.append(st_key)
                st_keys = ','.join(st_keys)
                obj['keyWords'] = st_keys

                source_as = dl.find_elements_by_xpath('./dd[3]/span[2]/a')
                for source_a in source_as:
                    st_a = source_a.get_attribute('textContent')
                    obj['source'] = st_a

                objs.append(obj)
                judge += 1
                print(judge)
            btn1 = driver.find_element_by_class_name('layui-laypage-next')
            btn1.click()
            time.sleep(2)

except Exception as e:
    print(e)

pd.DataFrame(objs).to_csv('paperInfo.csv', index=False)
